package com.samcai.swiperecyclerview;

import android.content.Context;
import android.support.annotation.ColorRes;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView.Adapter;
import android.util.AttributeSet;
import android.view.View;

import com.samcai.swiperecyclerview.imp.RecyclerViewImp;
import com.samcai.swiperecyclerview.listener.OnItemClickListener;
import com.samcai.swiperecyclerview.listener.OnItemLongClickListener;
import com.samcai.swiperecyclerview.listener.OnItemScrollLeftListener;
import com.samcai.swiperecyclerview.listener.OnItemScrollRightListener;
import com.samcai.swiperecyclerview.listener.OnItemSelectedListener;
import com.samcai.swiperecyclerview.listener.OnLoadInRecyclerListener;
import com.samcai.swiperecyclerview.listener.OnSubItemClickListener;
import com.samcai.swiperecyclerview.listener.OnSwipeRefreshListener;
import com.samcai.swiperecyclerview.view.CustomRecyclerView;
import com.samcai.swiperecyclerview.view.LoaderView;


/**
 * 结合RecyclerView的刷新控件
 * 
 * @author samcai-work
 * @time 2016年1月23日下午3:41:09
 * @version 1.0
 * @param <T>
 * @describe [nothing]
 */
public class SwipeRecyclerView extends SwipeRefreshLayout implements RecyclerViewImp, OnSwipeRefreshListener {

	/**
	 * 指示器颜色
	 */
	private static int[] indicatorColors = null;

	/**
	 * 指示器背景色
	 */
	@ColorRes
	private static int indicatorBgColor = -1;

	private CustomRecyclerView mRecyclerView;

	public SwipeRecyclerView(Context context) {

		super(context);
		init(context, indicatorBgColor, indicatorColors);
	}

	public SwipeRecyclerView(Context context, AttributeSet attrs) {

		super(context, attrs);
		init(context, indicatorBgColor, indicatorColors);
	}

	/**
	 * 获取RecyclerView实例
	 * 
	 * @return
	 */
	public final CustomRecyclerView getRecyclerView() {

		return mRecyclerView;
	}

	/**
	 * 初始化指示器颜色
	 * 
	 * @param color
	 */
	public static void setIndicatorColorRes(@ColorRes int... colors) {

		indicatorColors = colors;
	}

	/**
	 * 初始化指示器背景色
	 * 
	 * @param color
	 */
	public static void setIndicatorBGColorRes(@ColorRes int colorBg) {

		indicatorBgColor = colorBg;
	}

	/**
	 * 初始化刷新控件的颜色
	 * 
	 * @time 2016年1月23日下午3:16:44
	 * @param colorBg
	 *            指示器背景色，使用默认-1
	 * @param colors
	 *            指示器颜色，使用默认null
	 * @describe [nothing]
	 */
	public static void initColorsRes(@ColorRes int colorBg, @ColorRes int... colors) {

		setIndicatorColorRes(colors);
		setIndicatorBGColorRes(colorBg);
	}

	/*
	 * --------------------------------------------------------
	 * getProgressCircleDiameter 获取刷新圈圈直径 setColorSchemeColors(int...
	 * colors)设置刷新环颜色 setDistanceToTriggerSync(int distance) 手指移动多少触发刷新
	 * setProgressBackgroundColorSchemeColor (int color) 刷新指示器背景色
	 * setProgressBackgroundColorSchemeResource (int colorRes)刷新指示器背景色 setSize
	 * (int size) 加载指示器大小 DEFAULT, or LARGE. setProgressViewEndTarget (boolean
	 * scale, int end) 刷新指示器刷新的位置 setProgressViewOffset (boolean scale, int
	 * start, int end)刷新指示器初始位置、刷新位置
	 * --------------------------------------------------------
	 */

	@Override
	public void setAdapter(Adapter<?> adapter) {

		mRecyclerView.setAdapter(adapter);
	}

	@Override
	public void setOnItemClickListener(OnItemClickListener listener) {

		mRecyclerView.setOnItemClickListener(listener);
	}

	@Override
	public void setOnItemLongClickListener(OnItemLongClickListener listener) {

		mRecyclerView.setOnItemLongClickListener(listener);
	}

	@Override
	public void setOnItemSelectedListener(OnItemSelectedListener listener) {

		mRecyclerView.setOnItemSelectedListener(listener);
	}

	@Override
	public void setOnSubItemClickListener(int subViewId, OnSubItemClickListener listener) {

		mRecyclerView.setOnSubItemClickListener(subViewId, listener);
	}

	@Override
	public void setOnLoadListener(OnLoadInRecyclerListener listener) {

		mRecyclerView.setOnLoadListener(listener);
	}

	@Override
	public final void setOnItemLeftScrollListener(OnSwipeRefreshListener srListener, OnItemScrollLeftListener listener) {

		// Do nothing
	}

	@Override
	public final void setOnItemRightScrollListener(OnSwipeRefreshListener srListener, OnItemScrollRightListener listener) {

		// Do nothing
	}

	public void setOnItemLeftScrollListener(OnItemScrollLeftListener listener) {

		mRecyclerView.setOnItemLeftScrollListener(this, listener);
	}

	public void setOnItemRightScrollListener(OnItemScrollRightListener listener) {

		mRecyclerView.setOnItemRightScrollListener(this, listener);
	}

	@Override
	public View getHeaderView() {

		return mRecyclerView.getHeaderView();
	}

	@Override
	public View getFooterView() {

		return mRecyclerView.getFooterView();
	}

	@Override
	public LoaderView getLoadStateView() {

		return mRecyclerView.getLoadStateView();
	}

	@Override
	public void removeHeaderView(View headerView) {

		mRecyclerView.removeHeaderView(headerView);
	}

	@Override
	public void removeFooterView(View footerView) {

		mRecyclerView.removeFooterView(footerView);
	}

	@Override
	public void setHeaderView(View headerView) {

		mRecyclerView.setHeaderView(headerView);
	}

	@Override
	public void setFooterView(View footerView) {

		mRecyclerView.setFooterView(footerView);
	}

	@Override
	public void setItemDecoration(@ColorRes int color, int size, int leftMargin, int rightMargin) {

		mRecyclerView.setItemDecoration(color, size, leftMargin, rightMargin);
	}

	@Override
	public void setAutoLoadMoreEnable(boolean autoLoadMore) {

		mRecyclerView.setAutoLoadMoreEnable(autoLoadMore);
	}

	@Override
	public void setLoadStartText(String loadStartText) {

		mRecyclerView.setLoadStartText(loadStartText);
	}

	@Override
	public void setLoadSucceedText(String loadSucceedText) {

		mRecyclerView.setLoadSucceedText(loadSucceedText);
	}

	@Override
	public void setLoadFailedText(String loadFailedText) {

		mRecyclerView.setLoadFailedText(loadFailedText);
	}

	@Override
	public void setLoadNoMoreText(String loadNoMoreText) {

		mRecyclerView.setLoadNoMoreText(loadNoMoreText);
	}

	@Override
	public void succeed() {

		setRefreshing(false);
		mRecyclerView.succeed();
	}

	@Override
	public void failed() {

		setRefreshing(false);
		mRecyclerView.failed();
	}

	@Override
	public void setRefreshing(boolean arg0) {

		super.setRefreshing(arg0);
	}

	@Override
	public void noMore() {

		setRefreshing(false);
		mRecyclerView.noMore();
	}

	/**
	 * 设置指示器颜色，背景色，初始化RecyclerView
	 * 
	 * @param context
	 * @param bgColor
	 * @param colors
	 */
	private void init(Context context, @ColorRes int bgColor, @ColorRes int... colors) {

		// 优先自定义设置
		if (indicatorColors != null && indicatorColors.length > 0) {
			setColorSchemeResources(indicatorColors);

		} else if (colors != null && colors.length > 0) {
			setColorSchemeResources(colors);
		}

		// 优先自定义设置
		if (indicatorBgColor != -1) {
			setProgressBackgroundColorSchemeResource(indicatorBgColor);

		} else if (bgColor != -1) {
			setProgressBackgroundColorSchemeResource(bgColor);
		}

		//
		if (context != null) {
			mRecyclerView = new CustomRecyclerView(context);
			addView(mRecyclerView);
		}
	}

	@Override
	public void refreshEnable(boolean enabled) {

		setEnabled(enabled);
	}

}
